package springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springcloud.entities.CommonResult;
import springcloud.entities.Payment;
import springcloud.handler.CustomerBlockHandler;

@RestController
public class RateLimitController {
    @GetMapping(value = "/rateLimit/byResource")
    @SentinelResource(value = "byResource", blockHandler = "deal_byResource")
    public CommonResult byResource(){
        return new CommonResult(200,"测试成功",new Payment(200L,"资源名称限流"));
    }
    public CommonResult deal_byResource(BlockException exception){
        return new CommonResult(500,exception.getClass().getCanonicalName(),"服务不可用");
    }
    @GetMapping(value = "/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"测试成功",new Payment(200L,"Url限流"));
    }

    @GetMapping(value = "/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
                    blockHandlerClass = CustomerBlockHandler.class,
                    blockHandler = "handleException1")
    public CommonResult customerBlockHandler(){
        return new CommonResult(200,"测试成功",new Payment(200L,"自定义限流"));
    }

}
